<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page language='java'
	import='java.util.*,javax.servlet.http.HttpServletRequest,com.shenfa.bean.Person'%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%
Object obj=request.getSession().getAttribute("role_resource");
Person person = (Person) request.getSession().getAttribute("login");
if(null==obj){
	response.setStatus(1000);
	return;
}
%> 
	<script type="text/javascript">

		//素材列表数据源
		var data_store = new Ext.data.JsonStore({
    		root: "data",
    		totalProperty: "total",
    		fields: ["id","name","type","docname","begin","end","state","url","remark","personno","personname","createtime"],
    		url: "fodder!listsh.action"
    	});
    	//素材状态下拉列表
    	var state_store = new Ext.data.JsonStore({
			fields: ["id", "text"],
			data: [{id:"0",text:"退回"},{id:"1",text:"启用"},{id:"2",text:"草稿"},{id:"3",text:"审核中"}]
		});
    	//素材状态下拉列表
    	var state_store1 = new Ext.data.JsonStore({
			fields: ["id", "text"],
			data: [{id:"",text:"全部"},{id:"1",text:"启用"},{id:"3",text:"审核中"}]
		});
		//素材类别下拉列表
    	var type_store = new Ext.data.JsonStore({
			fields: ["id", "text"],
			data: [{id:"1",text:"图片"},{id:"2",text:"文本"}]
		});
    	//素材类别下拉列表
    	var type_store1 = new Ext.data.JsonStore({
			fields: ["id", "text"],
			data: [{id:"",text:"全部"},{id:"1",text:"图片"},{id:"2",text:"文本"}]
		});
			
			//定义数据显示面板
			var p1 = {
				id: "p1",
				xtype: "grid",
				stripeRows: true,
				loadMask: true,
				headersDisabled: true,
				autoExpandColumn: "name",
				store: data_store,
				columns:[
	    	    	new Ext.grid.RowNumberer({header: "序号", width: 35}),
    					{header: "编号",		dataIndex: "id", menuDisabled: true},
    					{header: "素材名称",	dataIndex: "name",	id: "name",menuDisabled: true},
    					{	header: "素材类型",width:60,menuDisabled: true,
    						dataIndex: "type",
    						renderer: function(data){
    							if("1" == data) return "图片";
    							else if("2" == data) return "文本";	
    						}
    					},
    					{header: "文件名",	dataIndex: "docname",menuDisabled: true},
    					{header: "有效期起始月",	dataIndex: "begin",menuDisabled: true},
    					{header: "有效期截止月",	dataIndex: "end",menuDisabled: true},
    					{	header: "状态",
    						width:50,
    						dataIndex: "state",
    						renderer: function(data){
    							if("0" == data) return "退回";
    							else if("1" == data) return "启用";
    							else if("2" == data) return "草稿";
    							else if("3" == data) return "审核中";
    						}
    					},
    					{header: "URL",	dataIndex: "url",menuDisabled: true},
    					{header: "备注",	dataIndex: "remark",menuDisabled: true},
    					{header: "操作人",	dataIndex: "personname",menuDisabled: true},
    					{header: "创建时间",	dataIndex: "createtime",menuDisabled: true},
    					{
    						xtype:"billActionColumn",
    						header: "预览",
    						menuDisabled: true,
    						width:34,
    						dataIndex: "type",
    						items:[{
			                    icon:"images/shared/icons/fam/information.png",  // Use a URL in the icon config
			                    tooltip:"预览",
			                    handler: function(grid, rowIndex, colIndex) {
									var rec = data_store.getAt(rowIndex);
									if("2"==rec.get("type"))return;
									var win=Ext.getCmp("showimages");
									win.show();
									win.update("<img id='imgpage' width="+win.getInnerWidth()+" height="+win.getInnerHeight()+" src='download.action?id="+encodeURI(rec.get('docname'))+"' />");
			                    }
			                }],
			                renderer: function(input){
    							return "1" == input;
    						}
    					}
				],
				tbar: ["&nbsp&nbsp素材类型:",
				 {
					xtype : "billCombox",
					width : 120,
					id : "foddertype",
					value:"",
					store : type_store1,
					listeners : {
						'select':function(){
							data_store.baseParams["fodder.type"] = this.value ;
							data_store.load({params:{start:0,limit:limit}});
							
						}
					}
				},"&nbsp&nbsp状态:",
				{
					xtype : "billCombox",
					width : 120,
					id : "fodderstate",
					value:"",
					store : state_store1,
					listeners : {
						'select':function(val){
							data_store.baseParams["fodder.state"] = this.value ;	
							data_store.load({params:{start:0,limit:limit}});						
						}
					}
				}],
				buttonAlign: "center",
				bbar: new Ext.ux.PagingToolbar({
    	        	pageSize:limit,
    	        	store: data_store
    	    	}),
				buttons:[{
					text:"查看详情", 
					handler: function(){
						var p1 = this.ownerCt.ownerCt;
						var panel = p1.ownerCt;
						var record = p1.getSelectionModel().getSelected();
						if (record){
							panel.getComponent("p2").setParam(record.data);
							oper_type = "upd";
							panel.layout.setActiveItem(1);
						}
						else
						{
						   Ext.Msg.alert("提示","未选中任何信息,无法进行此操作!");
						}			
					}
				}]
			};
			var p2 = {
				id: "p2",
			    labelWidth: 125,
				xtype: "form",
			    bodyStyle:"padding:5px 5px 0",
			    autoWidth: true,
				defaultType: "textfield",
				items: [{
					id: "fodder.id", 
					fieldLabel: "素材编号",	
					name: "fodder.id",
					anchor: "100%"
				},{
					id: "fodder.name", 
					fieldLabel: "素材名称",
					name: "fodder.name",
					anchor: "100%",
					validator:function (val){
		        		if(val=="")return true;
		        		if(Ext.fucCheckCHLength(val)>100){
		        			return "请输入100个字符范围内的值！<br>注：一个中文字符算两个字符的长度！";
		        		}
		        		return true;
		        	}
				},{
					id: "fodder.type", 	  
					fieldLabel: "素材类别",				
					name: "fodder.type",
					xtype: "billCombox",
					store: type_store
				},{
					id: "fodder.docname", 	  
					fieldLabel: "文件名",				
					name: "fodder.docname",
					anchor: "100%"
				},{
					id: "fodder.begin", 	  
					fieldLabel: "有效期起始月",				
					name: "fodder.begin",
					xtype: "datefield",
					format: "Y-m",
					editable: false,
					anchor: "100%"
				},{
					id: "fodder.end", 	  
					fieldLabel: "有效期截止月",				
					name: "fodder.end",
					xtype: "datefield",
					format: "Y-m",
					editable: false,
					anchor: "100%"
				},{
					id: "fodder.state", 	  
					fieldLabel: "状态",				
					name: "fodder.state",
					xtype: "billCombox",
					store: state_store
				},{
					id: "fodder.url", 	  
					fieldLabel: "URL",
					maxLength:400,
					name: "fodder.url",
					anchor: "100%"
				},{
					id: "fodder.remark", 	  
					fieldLabel: "备注",
					name: "fodder.remark",
					anchor: "100%"
				}],
			setParam: function(data){
				Ext.getCmp("fodder.id").setValue(data.id);
				Ext.getCmp("fodder.id").setDisabled(true);
				Ext.getCmp("fodder.id").setVisible(true);
				Ext.getCmp("fodder.name").setValue(data.name);
				Ext.getCmp("fodder.name").setDisabled(true);
				Ext.getCmp("fodder.type").setValue(data.type);
				Ext.getCmp("fodder.type").setDisabled(true);
				Ext.getCmp("fodder.docname").setValue(data.docname);
				Ext.getCmp("fodder.docname").setDisabled(true);
				Ext.getCmp("fodder.begin").setValue(data.begin);
				Ext.getCmp("fodder.begin").setDisabled(true);
				Ext.getCmp("fodder.end").setValue(data.end);
				Ext.getCmp("fodder.end").setDisabled(true);
				Ext.getCmp("fodder.state").setValue(data.state);
				Ext.getCmp("fodder.state").setDisabled(true);
				Ext.getCmp("fodder.url").setValue(data.url);
				Ext.getCmp("fodder.url").setDisabled(true);
				Ext.getCmp("fodder.remark").setValue(data.remark);
				Ext.getCmp("fodder.remark").setDisabled(true);
				//启用或用户自身提交审核中状态都不允许启用，退回
				if(data.personno == "<%=person.getId()%>" || data.state == 0 || data.state == 2) {
					Ext.getCmp("qy").setDisabled(true);
					Ext.getCmp("th").setDisabled(true);
				} else if(data.state == 1){	//启用状态只允许退回
					Ext.getCmp("qy").setDisabled(true);
					Ext.getCmp("th").setDisabled(false);
				} else {	//其他用户提交审核中，可启用，退回
					Ext.getCmp("qy").setDisabled(false);
					Ext.getCmp("th").setDisabled(false);
				}
				
			},
				buttonAlign: "center",
				buttons:[{id: "qy", text:"启用", handler: function(){
					var url,params;
					url="fodder!upd.action";
					params = {
						"fodder.id": Ext.getCmp("fodder.id").getValue(),
						"fodder.state":"1"
					};
					oper(url,params,Ext.getCmp("p1"));
				}},{id: "th", text:"退回", handler: function(){
					var url,params;
					url="fodder!upd.action";
					params = {
						"fodder.id": Ext.getCmp("fodder.id").getValue(),
						"fodder.state":"0"
					};
					oper(url,params,Ext.getCmp("p1"));
				}},{text:"取消", handler: function(){
					this.ownerCt.ownerCt.ownerCt.layout.setActiveItem(0);
				}
			}]
		};
		
		var fodderck_pel = new Ext.Panel({
				id: "mycontainer",
				border : false,	
				layout: "card",
				activeItem: 0,
				items: [p1, p2]
		});	

		
		var oper = function(url, params, panel){
			Ext.Ajax.request({
				clientValidation: true,
				params: params,
				url: url,
				success: function(response, opts){
					var d=eval("("+response.responseText+")");
					if(d.code == "1"){
						Ext.getCmp("p1").getBottomToolbar().doRefresh();
						Ext.Msg.show({
	   						title:"提示",
	   						msg:"操作成功!",
	   						buttons:Ext.Msg.OK,
	   						icon: Ext.MessageBox.INFO
						});
						Ext.getCmp("mycontainer").layout.setActiveItem(0);
					} else {
						Ext.Msg.show({
		   					title:"提示",
		   					msg:"操作失败!",
		   					buttons:Ext.Msg.OK,
		   					icon: Ext.MessageBox.ERROR
							});
					}
				},
				failure: function(response, opts){
					
				}
			});
		};	
					
		data_store.load({params:{start:0,limit:limit}});
		
		var CenterPanel=Ext.getCmp("CenterPanel");
		CenterPanel.removeAll();
		CenterPanel.add(fodderck_pel);
		CenterPanel.doLayout();
		
	</script>;
